package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.AppVersion;
import com.caocaog.market.mapper.AppVersionMapper;
import com.caocaog.market.service.AppVersionService;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * App版本控制管理
 *
 * @author chechen
 * @date 2022-03-01
 */
@Service
@Transactional
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public IPage<AppVersion> queryAppVersionPage(Page<AppVersion> page, AppVersion appVersion) {
        return super.baseMapper.queryAppVersionPage(page, appVersion);
    }

    @Override
    public String addAppVersion(AppVersion appVersion) {
        int insertRecordCount = super.baseMapper.insert(appVersion);
        if (insertRecordCount == 0 || appVersion.getId() == null)
            throw new JeecgBootException("新增App版本控制信息失败");
        return appVersion.getId().toString();
    }

    @Override
    public int updateAppVersion(AppVersion appVersion) {
        int updateRecordCount = super.baseMapper.updateById(appVersion);
        if (updateRecordCount == 0)
            throw new JeecgBootException("更新App版本控制信息失败, id-> " + appVersion.getId());
        return updateRecordCount;
    }

    @Override
    public int forcedUpdate(Integer appVersionId) {
        int updateRecordCount = super.baseMapper.update(null, new LambdaUpdateWrapper<AppVersion>()
                .setSql("is_forced_update = !is_forced_update")
                .eq(AppVersion::getId, appVersionId));
        if (updateRecordCount == 0)
            throw new JeecgBootException("更改强制更新失败, id-> " + appVersionId);
        return super.baseMapper.selectById(appVersionId).getIsForcedUpdate();
    }

    @Override
    public int using(Integer appVersionId) {
        int updateRecordCount = super.baseMapper.update(null, new LambdaUpdateWrapper<AppVersion>()
                .setSql("is_using = !is_using")
                .eq(AppVersion::getId, appVersionId));
        if (updateRecordCount == 0)
            throw new JeecgBootException("设置是否启用失败, id-> " + appVersionId);
        return super.baseMapper.selectById(appVersionId).getIsUsing();
    }

    @Override
    public int deleteAppVersion(Integer appVersionId) {
        int deleteRecordCount = super.baseMapper.deleteById(appVersionId);

        if (deleteRecordCount == 0)
            throw new JeecgBootException("删除App版本控制信息失败, id-> " + appVersionId);
        return deleteRecordCount;
    }

    @Override
    public AppVersion getNewAppVersion(Integer type, String siteId, String userId) {
        return super.baseMapper.getNewAppVersion(type, siteId, userId);
    }

}
